<?php 


/**
 * 
 */
class ExpresReports extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('reports_model');
	}



	public function index()
	{
		$xml_simple = simplexml_load_file('http://eds.expres.sk/kml/');
		$xml = $this->toArray($xml_simple);

		foreach ($xml as $key => $value) {
			$array_in = $value;
			foreach ($array_in as $key2 => $value2) {
				if ($key2 == "Placemark") {

					for ($i = 0; $i < count($value2); $i++) {
						if ($value2[$i]['kind'] == "Radar" || $value2[$i]['kind'] == "Nehoda" || $value2[$i]['kind'] == "Hliadka" || $value2[$i]['kind'] == "Mobilný radar") {
                    $time_seen = date("H:i:s d.m.Y", strtotime("+15 minutes", $value2[$i]['dateUpdate'])); // "+30 minutes", 
                    $type = $value2[$i]['kind'];

                    $xx = list($first, $second) = explode(',', $value2[$i]['Point']['coordinates']);
                    $lat = substr($xx[1], 0, 9);
                    $long = substr($xx[0], 0, 9);

                    $sel = $this->returnId($value2,$i);

                    $addr = $this->GetAddress($lat, $long);
                    $r_id = $sel['r_id'];
                    $row = $sel['row'];
                    $street_was = $this->getStreet($addr['addr']);
                    $city_was = $this->getCity($addr['addr']);
                    $state_was = $this->getState($addr['addr']);

                    $street_noaccent = $this->stripAccents($street_was);
                    $city_noaccent = $this->stripAccents($city_was);

                    if ($this->reports_model->checkExpres3Hours($street_was, $city_was)==0){


                    	$r = mt_rand(100, 9999);
                    	$r2 = mt_rand(100, 9999);

                    	$nova_lat = (float) $lat . $r;
                    	$nova_long = (float) $long . $r2;

                    	if ($value2[$i]['kind'] == "Mobilný radar") {
                    		$mob_rad = $this->nazov_auta($value2[$i]['description']);
                    	} else {
                    		$mob_rad = "";
                    	}

                    	$this->reports_model->insertExpres('100',
                    		$r_id,
                    		$nova_lat,
                    		$nova_long,
                    		$street_was,
                    		$city_was,
                    		$state_was,
                    		$street_noaccent,
                    		$city_noaccent,
                    		date("Y-m-d H:i:s", strtotime($time_seen)),
                    		$this->AreaToId($addr['region']),
                    		$mob_rad,
                    		'Rádio Expres'
                    		);



                        // if ($sel['r_id'] == 3) {
                        //     $db->query("UPDATE users SET $row=$row+3 where user_id='100'");
                        // } else {
                        //     $db->query("UPDATE users SET $row=$row+1 where user_id='100'");
                        // }
                    	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
                    	echo $this->getStreet($addr['addr']) . $this->getCity($addr['addr']) . $this->getState($addr['addr']) ." - ". $addr['region'] . "<br />";
                    // }
                    }
                }
            }
        }
    }


}
}


private function GetAddress($lat, $lng) {
	$URL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false";
	$data = file($URL);
	$pocitadlo=0;
	foreach ($data as $line_num => $line) {
		if (strstr($line,'Region') && $pocitadlo==0){
			$region = substr($line,30);
			$region = substr_replace($region, '', -10);
			$pocitadlo++;
		}

		if (false != strstr($line, 'formatted_address')) {
			$addr = substr(trim($line), 23, -2);
			if (empty($region)){
				$pocitadlo=0;
				foreach ($data as $line_num => $line) {
					if (strstr($line,'Region') && $pocitadlo==0){
						$region = substr($line,30);
						$region = substr_replace($region, '', -10);
						$pocitadlo++;
					}
				}
			}
			break;
		}
	}
	return array('addr' => $addr, 'region' => $region);
}


private function toArray($xml) {
	$array = json_decode(json_encode($xml), TRUE);

	foreach (array_slice($array, 0) as $key => $value) {
		if (empty($value))
			$array[$key] = NULL;
		elseif (is_array($value))
			$array[$key] = $this->toArray($value);
	}
	return $array;
}

private function AreaToId($area) {
	$area = trim($area);
	switch ($area) {
		case 'Prešov':
		$area_id = 4;
		break;
		case 'Žilina':
		$area_id = 8;
		break;
		case 'Nitra':
		$area_id = 9;
		break;
		case 'Trenčín':
		$area_id = 5;
		break;
		case 'Košice':
		$area_id = 3;
		break;
		case 'Bratislava':
		$area_id = 7;
		break;
		case 'Banská Bystrica':
		$area_id = 2;
		break;
		case 'Trnava':
		$area_id = 6;
		break;
		default:
		$area_id = 1;
		break;
	}
	return $area_id;
}



private function returnId($value2,$i) {
	if ($value2[$i]['kind'] == "Mobilný radar") {
		$r_id = 4;
		$row = 'num_of_mobile_radars';
	} else if ($value2[$i]['kind'] == "Radar") {
		$r_id = 1;
		$row = 'num_of_radars';
	} else if ($value2[$i]['kind'] == "Nehoda") {
		$r_id = 3;
		$row = 'num_of_accidents';
	} else if ($value2[$i]['kind'] == "Hliadka") {
		$r_id = 2;
		$row = 'num_of_patrols';
	}
	return array('r_id' => $r_id, 'row' => $row);
}

private function nazov_auta($string) {
	$string = explode(' ', $string);
	return $string[count($string) - 2] . " " . $string[count($string) - 1];
}


public function getStreet($input){
	$part = explode(",",$input);
	if (count($part)==4){
		return $part[1];
	}
	else
	{
		return $part[0];
	}
}

public function getCity($input){
	$part = explode(",",$input);
	if (count($part)==3){
		return ltrim($part[1]);
	}
	else if (count($part)==4){
		return ltrim($part[2]);
	}
	else
	{
		return null;
	}

}

public function getState($input){
	$part = explode(",",$input);
	if (count($part)==3){
		return ltrim($part[2]);
	}
	else if (count($part)==4){
		return ltrim($part[3]);
	}
	else
	{
		return ltrim($part[1]);
	}

}

function stripAccents($str)
{
	$a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', '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u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
	return str_replace($a, $b, $str);
}	



} 




?>